open BUFF,"<merge_expr_filtered" or die "no file exists merge_expr_filtered\n";
open BUFF1,"<combined_GBM_normal_plink_final_filetered_het_minhomo_10.tped" or die "no file exists combined_GBM_normal_plink_final_filetered_het_minhomo_10.tped\n";
$samplesize = 20;
$line = <BUFF>;
chomp($line);
#create sample array expr
@gene_samples = split(/\t/,$line);
#shift(@gene_samples);
$length = @gene_samples;
#print $length."\n";
@value=split(/\./,$length/$samplesize);
#extracting the integer part
$value = $value[0];

if($value < 1)
{
	$value = 1;
}
#read the first line in tped file for sample names
$line = <BUFF1>;
chomp($line);
#create sample array
@geno_samples = split(" ",$line);
my @group_expr;
my @group_geno;
$j =0;
#create gene expression and geno typing sub part file names 
for($i=0;$i<$value;$i++)
{
	$val = $i+1;
	$group_expr[$i] = "expr_part_$val";
	$group_geno[$i] = "geno_part_$val";
	$buff = "WREXPR$val";
	open($buff,">./temp/$group_expr[$i]");
	$buff = "WRGENO$val";
	open($buff,">./temp/$group_geno[$i]");
}
$samplenumber =0;
$val = 1;
$buff = "WREXPR$val";
$buff1 = "WRGENO$val";

#split the sample names and write on the sub files (only sample names)
for($i=0;$i<@gene_samples;$i++)
{
	if($samplenumber == $samplesize && $val != $value)
        {
		$samplenumber =0;
		print $buff "\n";
		print $buff1 "\n";
		$val++;
		$buff = "WREXPR$val";
		$buff1 = "WRGENO$val";	
        }

	print 	$buff "\t".$gene_samples[$i];
	print  $buff1 "\t".$geno_samples[$i];
	$samplenumber++;
}
print $buff "\n";
print $buff1 "\n";
#$samplenumber =0;
#$val = 1;
#$buff = "WREXPR$val";
#$line = <BUFF>;

#split each gene in to sub part files
while($line = <BUFF>)
{
	$samplenumber =0;
	$val = 1;
	$buff = "WREXPR$val";
	chomp($line);
	@expr = split(/\t/,$line);
	print $buff $expr[0];
	for($i=1;$i<@expr;$i++)
	{
		if($samplenumber == $samplesize && $val != $value)
		{
			$samplenumber =0;
			print $buff "\n";
			$val++;
			$buff = "WREXPR$val";
			print $buff $expr[0];
		}
		print $buff "\t".$expr[$i]; 
		$samplenumber++;
	}
	print $buff "\n";
}


#split each genotype in to sub part files
while($line = <BUFF1>)
{
        $samplenumber =0;
        $val = 1;
        $buff1 = "WRGENO$val";
        chomp($line);
	@geno = split(/ /,$line);
	print $buff1 $geno[0]." ".$geno[1]." ".$geno[2]." ".$geno[3]." ".$geno[4]." ".$geno[5]." ".$geno[6];
	for($i=7;$i<@geno;$i++)
        {
                if($samplenumber == $samplesize && $val != $value)
                {
                        $samplenumber =0;
                        print $buff1 "\n";
                        $val++;
                        $buff1 = "WRGENO$val";
                        print $buff1 $geno[0]." ".$geno[1]." ".$geno[2]." ".$geno[3]." ".$geno[4]." ".$geno[5]." ".$geno[6];
                }
                print $buff1 " ".$geno[$i];
		#$i++;
		#print $buff1 " ".$geno[$i];
                $samplenumber++;
        }
        print $buff1 "\n";
}
for($i=1;$i<$value+1;$i++)
{
	$buff = "WREXPR$i";
	$buff1 = "WRGENO$i";
	close($buff);
	close($buff1);
}
